package jicheng;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xx.jicheng.mapper.ProductMapper;

public class SpiderImage {

	private static String SavePath = "E:\\workspace\\jicheng\\src\\main\\webapp\\root\\upload4\\";
	private static BlockingQueue<String> queue = new LinkedBlockingQueue<>();
	private static final AtomicInteger count = new AtomicInteger(0);
	private static long begin;

	public static void main(String[] args) throws InterruptedException {
		

		ExecutorService service = Executors.newFixedThreadPool(20);
		for(int i = 0; i < 1;i++){
			Runnable task = newTask();
			service.submit(task);
		}
		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
		ProductMapper productMapper = context.getBean(ProductMapper.class);
		List<String> images = productMapper.findAllImage();
		int i = 0;
		begin = System.currentTimeMillis();
		for(String img : images) {
			queue.put(img);
			i++;
			if(i>=100){
				break;
			}
		}
		context.close();
	}

	

	private static Runnable newTask() {
		Runnable task = new Runnable() {

			@Override
			public void run() {
				while (true) {
					InputStream is = null;
					OutputStream os = null;
					try {
						String url = queue.take();
						int c = count.incrementAndGet();
						if(c == 100){
							System.out.println(System.currentTimeMillis()-begin);
						}
						System.out.println(c + Thread.currentThread().getName());
						System.out.println(url);
						String filename = url.substring(url.lastIndexOf("/") + 1);
						if (url.startsWith("http")) {
							HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

							conn.connect();
							is = conn.getInputStream();
							File f = new File(SavePath + filename);
							if(f.exists()){
								continue;
							}
							os = new FileOutputStream(SavePath + filename);
							byte[] buf = new byte[1024 * 256];
							int len = -1;
							while (true) {
								len = is.read(buf);
								if (len == -1) {
									break;
								}
								os.write(buf, 0, len);
							}
						}

					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						if (is != null) {
							try {
								is.close();
							} catch (IOException e) {
							}
						}
						if (os != null) {
							try {
								os.close();
							} catch (IOException e) {
							}
						}
					}

				}

			}
		};
		return task;
	}

}
